package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	
	import org.osflash.signals.Signal;
	
	public class ContentArrowControl extends Sprite
	{
		private var gap:uint = 5;
		public var data:Object;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var baseRedColor:uint = 0xC52027;
		private var borderColor:uint = 0xAAAAAA;
		
		private var main:Sprite = new Sprite();
		private var base:Sprite = new Sprite();
		private var over:Sprite = new Sprite();
		private var press:Sprite = new Sprite();
		private var panel:Sprite;
		private var leftSense:Boolean = false;
		
		public var onClicked:Signal = new Signal(ContentArrowControl);
		
		public function ContentArrowControl(data:Object, vWidth:uint, vHeight:uint, leftSense:Boolean = false)
		{
			super();
			this.data = data;
			this.controlWidth = vWidth;
			this.controlHeight = vHeight;
			this.leftSense = leftSense;
			addChild(main);
			main.addChild(base);
			main.addChild(over);
			main.addChild(press);
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			display();
		}
		
		private function display():void
		{
			buildBase();
			buildOver();
			buildPress();
			addListeners();
		}
		
		private function build(item:Sprite, bgColor:uint, textColor:uint = 0x1f416c):void
		{
			setListItemBase(item, bgColor, textColor);
		}
		
		private function setListItemBase(item:Sprite, bgColor:uint, textColor:uint = 0x1f416c):void
		{
			//item.graphics.lineStyle(0.5, borderColor);
			item.graphics.beginFill(bgColor);
			item.graphics.drawRect(0, 0, controlWidth, controlHeight);
			item.graphics.endFill();
			
			var arrow:Sprite = buildArrowSymbol(textColor, leftSense ? 0 : 1);
			arrow.x = item.width/2 - arrow.width/2;
			arrow.y = item.height/2 - arrow.height/2;
			item.addChild(arrow);

			item.alpha = 0;
		}
		
		private function addListeners():void
		{
			press.addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.addEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function removeListeners():void
		{
			press.removeEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.removeEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.removeEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function onRollOver(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,1,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRollOut(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,2,{ease:Strong.easeOut,alpha:0});
			press.alpha = 0;
		}
		
		private function onPress(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,0.5,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRelease(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,1,{ease:Strong.easeOut,alpha:0});
			onClicked.dispatch(this);
		}
		
		private function buildBase():void
		{
			build(base, 0xDBDBD1, baseRedColor);
			base.alpha = 1;
		}
		
		private function buildOver() : void
		{
			build(over, 0xffffff, baseRedColor);
		}
		
		private function buildPress() : void
		{
			build(press, 0xffffff, baseRedColor);
		}
		
		public function dispose():void
		{
			removeListeners();
		}
		
		private function buildArrowSymbol(color:uint, sense:uint):Sprite
		{
			var aWidth:uint = 8;
			var aHeight:uint = 12;
			var sprite:Sprite = new Sprite();
			sprite.graphics.lineStyle(1, color);
			if(sense == 0)
			{
				sprite.graphics.lineTo(0,0);
				sprite.graphics.lineTo(aWidth,aHeight/2);
				sprite.graphics.lineTo(0,aHeight);
			} else {
				sprite.graphics.moveTo(aWidth,0);
				sprite.graphics.lineTo(0,aHeight/2);
				sprite.graphics.lineTo(aWidth,aHeight);
			}
			return sprite;
		}
	}
}